using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.SXXK;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;

namespace Company.Interface.KDT.SXXK
{
    public partial class SelectSanPhamDMForm : BaseForm
    {
        public SanPham SanPhamSelected = new SanPham();

        public bool status = true;//0 mo moi hoan toan//1 da co roi mo len
        private DataSet dsRegistedList;
        public SelectSanPhamDMForm()
        {
            InitializeComponent();
            this.ctrDonViHaiQuan.ValueChanged -= new Company.Interface.Controls.DonViHaiQuanControl.ValueChangedEventHandler(this.ctrDonViHaiQuan_ValueChanged);
            dsSanPham.DropDownList.LoadingRow += new Janus.Windows.GridEX.RowLoadEventHandler(this.dsSanPham_LoadingRow);
            this.dsSanPham.ValueChanged -= new System.EventHandler(this.dsSanPham_ValueChanged);       
        }
        public void BindDanhSachSP(string mahq)
        {
            string where = "TrangThaiXuLy<>1";
            if (mahq != "")
                where += " and MaHaiQuan='"+mahq+"'";
            Company.BLL.KDT.SXXK.SanPhamDangKyCollection collection = Company.BLL.KDT.SXXK.SanPhamDangKy.SelectCollectionDynamic(where, "");
            Company.BLL.KDT.SXXK.SanPhamDangKy spdk = new Company.BLL.KDT.SXXK.SanPhamDangKy();
            spdk.ID = 0;
            spdk.SoTiepNhan = 0;
            collection.Add(spdk);
            dsSanPham.DataSource = collection;
            dsSanPham.DisplayMember = "ThongTin";
            dsSanPham.ValueMember = "ID";
            dsSanPham.SelectedIndex = collection.Count - 1;            
        }
        public void BindData()
        {
            long id = Convert.ToInt64(dsSanPham.Value.ToString());
            if (id == 0)
            {
                dgList.DataSource = new SanPham().SearchBy_MaHaiQuan(ctrDonViHaiQuan.Ma).Tables[0];                
            }
            else
            {
                Company.BLL.KDT.SXXK.SanPhamCollection spCollection = Company.BLL.KDT.SXXK.SanPham.SelectCollectionBy_Master_ID(id);
                dgList.DataSource = spCollection;                
            }
            
        }

        private void dsSanPham_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (e.Row.Cells["TrangThaiXL"].Text == "0" && e.Row.Cells["ID"].Text != "0")
                    e.Row.Cells["TrangThaiXL"].Text = "Chờ duyệt";
                else if (e.Row.Cells["TrangThaiXL"].Text == "-1" && e.Row.Cells["ID"].Text != "0")
                    e.Row.Cells["TrangThaiXL"].Text = "Chưa khai báo";

                if (e.Row.Cells["ID"].Text != "0")
                {
                    e.Row.Cells["ThongTin"].Text = "Danh sách sản phẩm " + e.Row.Cells["ID"].Text;                    
                }
                else
                {
                    e.Row.Cells["ThongTin"].Text = "Danh sách đã duyệt";                    
                }
            }
        }
        //-----------------------------------------------------------------------------------------
        
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            if (this.CalledForm == "DinhMucSendForm")
            {
                ctrDonViHaiQuan.Enabled = false;
            }
            lblHint.Visible = this.CalledForm != string.Empty;
        }

        //-----------------------------------------------------------------------------------------
        
        private void SanPhamRegistedForm_Load(object sender, EventArgs e)
        {
            if (status)
            {
                this.khoitao_DuLieuChuan();
                BindDanhSachSP(ctrDonViHaiQuan.Ma);
                // Sản phẩm đã đăng ký.
                this.BindData();
                // Doanh nghiệp / Đại lý TTHQ.
                dgList.Tables[0].Columns["MaDoanhNghiep"].Visible = GlobalSettings.DAI_LY_TTHQ;
                this.ctrDonViHaiQuan.ValueChanged += new Company.Interface.Controls.DonViHaiQuanControl.ValueChangedEventHandler(this.ctrDonViHaiQuan_ValueChanged);
                this.dsSanPham.ValueChanged += new System.EventHandler(this.dsSanPham_ValueChanged);      
                status = false;
            }
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
                e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                btnGetListFromHQ.Enabled = false;
                this.dsRegistedList = new SanPham().WS_GetDanhSachDaDangKy(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
                dgList.DataSource = this.dsRegistedList.Tables[0];
                // Cập nhật vào CSDL.
                bool ret = new SanPham().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, this.dsRegistedList);
                if (ret)
                {
                    this.ShowMessage("Cập nhật thành công", false);
                }
                BindDanhSachSP(ctrDonViHaiQuan.Ma);
            }
            catch (Exception ex)
            {
                this.ShowMessage("Có lỗi: " + ex.Message, false);
            }
            finally
            {
                btnGetListFromHQ.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (lblHint.Visible)
            {
                if (e.Row.RowType == RowType.Record)
                {
                    string maSP = e.Row.Cells["Ma"].Text;
                    this.SanPhamSelected.Ma = maSP;
                    this.SanPhamSelected.MaHaiQuan = this.MaHaiQuan;
                    this.SanPhamSelected.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    this.SanPhamSelected.Ten = e.Row.Cells["Ten"].Text;
                    this.SanPhamSelected.MaHS = e.Row.Cells["MaHS"].Text;
                    this.SanPhamSelected.DVT_ID = e.Row.Cells["DVT_ID"].Text;
                    this.SanPhamSelected.Load();
                    this.Hide();
                }
            }
            else 
            {

            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.dsSanPham.ValueChanged -= new System.EventHandler(this.dsSanPham_ValueChanged);       
            long id = Convert.ToInt64(dsSanPham.Value.ToString());
            BindDanhSachSP(ctrDonViHaiQuan.Ma);
            if (id == 0)
                dgList.DataSource = new SanPham().SearchBy_MaHaiQuan(ctrDonViHaiQuan.Ma).Tables[0];
            else
            {                
                BindData();
            }
            this.dsSanPham.ValueChanged += new System.EventHandler(this.dsSanPham_ValueChanged);
                 
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void dsSanPham_ValueChanged(object sender, EventArgs e)
        {
            this.ctrDonViHaiQuan.ValueChanged -= new Company.Interface.Controls.DonViHaiQuanControl.ValueChangedEventHandler(this.ctrDonViHaiQuan_ValueChanged);          
            BindData();
            this.ctrDonViHaiQuan.ValueChanged += new Company.Interface.Controls.DonViHaiQuanControl.ValueChangedEventHandler(this.ctrDonViHaiQuan_ValueChanged);          
        }

        //-----------------------------------------------------------------------------------------
    }
}